<?php
namespace app\admin\service;

/**
 * 服务基类
 */
class BaseService
{
    // 模型
    protected $model;
	//默认查询条件
    protected $where;
	//关联
    protected $with;
	//默认排序
    protected $order;
	//只查询字段
  	protected $field = [];
	//排除字段
    protected $withoutField=[];
	//只取多少条
    protected $limit;
	
	//设置关联信息
    public function with($with){
    	$this->with = $with;
		return $this;
    }
	
	//设置排除字段
    public function withoutField($fields=[]){
    	$this->withoutField = $fields;
		return $this;
    }
	
	//设置排序字段
    public function order($fields=""){
    	$this->order = $fields;
		return $this;
    }
	
	//设置只查询字段
    public function field($fields="*"){
    	$this->field = $fields;
		return $this;
    }
	
	//设置只查询字段
    public function limit($num=20){
    	$this->limit = $num;
		return $this;
    }
	
	 /**
     * 查询列表数据
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getList($where=[]){
        $model = $this->model->where($where)->withoutField($this->withoutField);
		if($this->with){
			$model = $model->with( $this->with );
		}
		if($this->field){
			$model = $model->field( $this->field );
		}	
		if($this->where){
			$model = $model->where( $this->where );
		}	
		if($this->order){
			$model = $model->order( $this->order );
		}	
		if($this->limit){
			$model = $model->limit($this->limit);
		}
        return $model->select()->toArray();
    }

    /**
     * 查询分页数据
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getPageList($where=[],$page='',$limit=''){
    	$page = $page ?: request()->param("pageIndex",1);
        $limit = $limit ?: request()->param("pageSize",20);
		
        $model = $this->model->where($where)->withoutField($this->withoutField);
		if($this->with){
			$model = $model->with( $this->with );
		}
		if($this->field){
			$model = $model->field( $this->field );
		}	
		if($this->where){
			$model = $model->where( $this->where );
		}	
		if($this->order){
			$model = $model->order( $this->order );
		}	

        return $model->paginate(['page' => $page,'list_rows'=> $limit])->toArray();
    }

    /**
     * 根据id查询明细
     * @param $id
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfoById($id){
		$model = $this->model->where('id',$id)->withoutField($this->withoutField);
		if($this->with){
			$model = $model->with( $this->with );
		}
		if($this->field){
			$model = $model->field( $this->field );
		}	
		if($this->where){
			$model = $model->where( $this->where );
		}	
		$res = $model->find();
        return $res;
    }

    /**
     * 新增
     * @param $params
     * @return RosterModel|\think\Model
     */
    public function add($params){
        $res = $this->model->createInfo($params);
        return $res;
    }
	
	 /**
     * 新增s
     * @param $params
     * @return RosterModel|\think\Model
     */
    public function adds($params){
        $res = $this->model->saveAll($params);
        return $res;
    }
	
    /**
     * 编辑
     * @param $params
     * @return bool
     */
    public function update($params){
        $res = $this->model->updateInfo($params['id'],$params);
        return $res;
    }

	/**
     * 删除
     * @param $params
     * @return bool
     */
    public function delete($id){
        $res = $this->model->where('id',$id)->delete();
        if ($res){
            return true;
        }
        return false;
    }
	
	
	/**
     * 删除
     * @param $params
     * @return bool
     */
    public function deletes($where){
        $res = $this->model->where($where)->delete();
        if ($res){
            return true;
        }
        return false;
    }
	
}